package org.otherworld.frame.filebrowser.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

import org.otherworld.frame.filebrowser.tree.FileTreeNode;

public class FileBrowserDelete implements ActionListener {

	private JPanel panel;
	private JTree tree;
	private File[] editPath;

	public FileBrowserDelete(JPanel panel, JTree tree) {
		this.panel = panel;
		this.tree = tree;
	}

	public void actionPerformed(ActionEvent e) {
		JTree tree = this.tree;
		TreePath[] paths = tree.getSelectionPaths();
		String name = tree.getName();
		this.editPath = new File[paths.length];
		if (paths != null) {
			for (int i = 0; i < paths.length; i++) {
				FileTreeNode node = (FileTreeNode) paths[i]
						.getLastPathComponent();
				this.editPath[i] = node.getFile();
				System.out.println(name + " path: " + node.getFile());
			}
		} else {
			System.out.println("Path null");
		}
		int result = JOptionPane.showConfirmDialog(tree, "Are you sure to delete this files?");
		File[] files = this.editPath;
		File temp;
		if (result == 0) {
			for (int i = 0; i < files.length; i++) {
				temp = files[i];
				temp.delete();
			}
		}
		tree.updateUI();

	}
}
